package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.basic.exception.BizException;
import top.tangyh.lamp.care.entity.*;
import top.tangyh.lamp.care.mapper.*;
import top.tangyh.lamp.care.service.BHl7YzService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BHl7YzManager;
import top.tangyh.lamp.care.vo.query.*;
import top.tangyh.lamp.care.vo.result.LisResultOutVO;
import top.tangyh.lamp.care.vo.result.OperaInfoOutVO;
import top.tangyh.lamp.care.vo.result.ProjectDetailOutVO;
import top.tangyh.lamp.care.vo.save.BHl7YzSaveVO;
import top.tangyh.lamp.care.vo.save.OperaInfoInVO;
import top.tangyh.lamp.care.vo.update.BHl7YzUpdateVO;
import top.tangyh.lamp.care.vo.result.BHl7YzResultVO;
import top.tangyh.lamp.common.constant.YzTypeEnum;
import top.tangyh.lamp.common.utils.PDateUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static top.tangyh.lamp.common.constant.CommonConstant.YES_NUMBER;

/**
 * <p>
 * 业务实现类
 * 医嘱表包含用药，检验，检查
 * </p>
 *
 * @author zqb
 * @date 2024-07-15 10:48:09
 * @create [2024-07-15 10:48:09] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BHl7YzServiceImpl extends SuperServiceImpl<BHl7YzManager, Long, BHl7Yz> implements BHl7YzService {

    private final BHl7YzMapper bHl7YzMapper;

    private final BHl7LisBarMapper bHl7LisBarMapper;

    private final BHl7YzYaoExeMapper bHl7YzYaoExeMapper;


    private final BHl7YzLisResultMapper bHl7YzLisResultMapper;

    private final BHl7YzPacsResultMapper bHl7YzPacsResultMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOperaInfo(OperaInfoInVO operaInfoVO) {
        BHl7Yz yzPO = new BHl7Yz();
        yzPO.setPatientIdent(operaInfoVO.getPatientIdent());
        yzPO.setHospitalNumber(operaInfoVO.getHospitalNumber());
        yzPO.setTypeIdent(YzTypeEnum.SHOUSHU.getTypeCode());
        yzPO.setOrderName(operaInfoVO.getOperaName());
        yzPO.setPlanTime(PDateUtils.getDateTimeOfTimestamp(operaInfoVO.getOperaTime()));
        yzPO.setIsDiy(YES_NUMBER);
        bHl7YzMapper.insert(yzPO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOperaInfo(OperaInfoInVO operaInfoVO) {
        BHl7Yz yzPO = bHl7YzMapper.selectById(operaInfoVO.getId());
        if (yzPO != null) {
            BHl7Yz updateYzPO = new BHl7Yz();
            updateYzPO.setId(operaInfoVO.getId());
            updateYzPO.setOrderName(operaInfoVO.getOperaName());
            updateYzPO.setPlanTime(PDateUtils.getDateTimeOfTimestamp(operaInfoVO.getOperaTime()));
            bHl7YzMapper.updateById(updateYzPO);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOperaInfo(Long id) {
        BHl7Yz bHl7Yz = bHl7YzMapper.selectById(id);
        if (bHl7Yz != null) {
            BHl7Yz yzPO = new BHl7Yz();
            yzPO.setId(id);
            yzPO.setIsDeleted(YES_NUMBER);
            bHl7YzMapper.updateById(yzPO);
        }
    }

    @Override
    public List<OperaInfoOutVO> listOperaInfo(String hospitoalNumber) {
        List<OperaInfoOutVO> operaInfoOutVOListAll = bHl7YzMapper.selectListByHospitalNumberAndType(hospitoalNumber, YzTypeEnum.SHOUSHU.getTypeCode()).stream().map(OperaInfoOutVO::new).collect(Collectors.toList());

        return operaInfoOutVOListAll;
    }

    @Override
    public List<BHl7Yz> selectYzListByQuery(YzQueryVO queryInVO) {
        return bHl7YzMapper.selectListByQuery(queryInVO);
    }

    @Override
    public List<BHl7LisBar> selectLisBarByQuery(ListBarQueryVO listBarQueryVO) {
        return bHl7LisBarMapper.selectLisBarByQuery(listBarQueryVO);
    }

    @Override
    public List<BHl7YzYaoExe> selectYaoExeListByQuery(YzYaoExeQueryVO queryInVO) {
        return bHl7YzYaoExeMapper.selectListByQuery(queryInVO);
    }

    @Override
    public List<BHl7YzLisResult> selectLisResultByQuery(YzListResultQueryVO queryVO) {
        return bHl7YzLisResultMapper.selectListByQuery(queryVO);
    }

    @Override
    public List<BHl7YzPacsResult> selectPacsResultByQuery(YzPacsResultQueryVO queryVO) {
        return bHl7YzPacsResultMapper.selectListByQuery(queryVO);
    }

    @Override
    public List<LisResultOutVO> selectListDetailByQuery(LisDetailQueryVO queryVO) {
        if (CollectionUtil.isEmpty(queryVO.getProjectList())) {
            throw new BizException("请输入检验项目ident集合");
        }
        Map<String, List<ProjectDetailOutVO>> detailMap = bHl7YzLisResultMapper.selectListDetailByQuery(queryVO).stream().map(ProjectDetailOutVO::new).collect(Collectors.groupingBy(ProjectDetailOutVO::getProjectName));
        List<LisResultOutVO> resultList = new ArrayList<>();
        detailMap.forEach((projectName, list) -> {
            LisResultOutVO resultOutVO = new LisResultOutVO();
            resultOutVO.setProjectName(projectName);
            resultOutVO.setReportName(list.get(0).getReportName());
            List<ProjectDetailOutVO> detailList = list.stream().sorted(Comparator.comparing(ProjectDetailOutVO::getTakeTime)).collect(Collectors.toList());
            resultOutVO.setDetailList(detailList);
            resultList.add(resultOutVO);
        });
        return resultList;
    }

}


